package com.android.jay.lib_audio.view;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.widget.RemoteViews;

import com.android.jay.lib_audio.R;
import com.android.jay.lib_audio.app.AudioHelper;
import com.android.jay.lib_audio.mediaplayer.core.AudioController;
import com.android.jay.lib_audio.mediaplayer.core.MusicService;
import com.android.jay.lib_audio.mediaplayer.db.GreenDaoHelper;
import com.android.jay.lib_audio.mediaplayer.model.AudioBean;
import com.android.jay.lib_image_loader.app.ImageLoaderManager;

import androidx.core.app.NotificationCompat;

/**
 * Created by wzp on 2020/1/15 0015.
 * <p>
 * 描述  ：音乐notification帮助类
 */
public class NotificationHelper {
    /**
     * 1.完成notification的创建和初始化
     * 2.对外提供更新notification的方法
     **/

    public static final String CHANNEL_ID = "channel_lib_audio";
    public static final String CHANNEL_NAME = "channel_name_audio";
    public static final int NOTIFICATION_ID = 0X111;

    /**
     * ui相关
     **/

    private Notification mNotification;
    private RemoteViews mRemoteView; //大布局
    private RemoteViews mSmallRemoteView; //小布局
    private NotificationManager mNotificationManager;


    /**
     * data
     **/

    private String packageName;
    private NotificationHelperListener mListener;
    private AudioBean mAudioBean;

    public static NotificationHelper getInstance() {
        return SingletonHolder.instance;
    }

    private static class SingletonHolder {
        private static NotificationHelper instance = new NotificationHelper();
    }

    /**
     * 与音乐service的回调通信
     */
    public interface NotificationHelperListener {
        void onNotificationInit();
    }


    public void init(NotificationHelperListener listener) {
        mNotificationManager =
                (NotificationManager) AudioHelper.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        packageName = AudioHelper.getContext().getPackageName();
        mAudioBean = AudioController.getInstance().getNowPlaying();
        initNotification();
        mListener = listener;
        if (mListener != null) mListener.onNotificationInit();
    }

    private void initNotification() {
        if (mNotification == null) {
            //首先创建布局
            initRemoteViews();
            //在构建Notification
            Intent intent = new Intent(AudioHelper.getContext(), MusicPlayerActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(AudioHelper.getContext(),
                    0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            //适配安卓8.0的消息渠道
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel
                        = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME
                        , NotificationManager.IMPORTANCE_HIGH);
                //不提示灯
                channel.enableLights(false);
                //不震动
                channel.enableVibration(false);
                mNotificationManager.createNotificationChannel(channel);
            }

            NotificationCompat.Builder builder = new NotificationCompat.Builder(AudioHelper.getContext(), CHANNEL_ID)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setCustomBigContentView(mRemoteView) //大布局
                    .setContent(mSmallRemoteView); // 正常布局
            mNotification = builder.build();

            showLoadingStatus(mAudioBean);
        }


    }

    /***
     * 创建notification 布局
     * **/
    private void initRemoteViews() {

        int layoutId = R.layout.notification_big_layout;
        mRemoteView = new RemoteViews(packageName, layoutId);
        mRemoteView.setTextViewText(R.id.title_view, mAudioBean.name);
        mRemoteView.setTextViewText(R.id.tip_view, mAudioBean.album);

        //是否被收藏
        if (GreenDaoHelper.selectFavourite(mAudioBean) != null) {
            mRemoteView.setImageViewResource(R.id.favourite_view, R.mipmap.note_btn_loved);
        } else {
            mRemoteView.setImageViewResource(R.id.favourite_view, R.mipmap.note_btn_love_white);
        }


        mSmallRemoteView = new RemoteViews(packageName, layoutId);
        mSmallRemoteView.setTextViewText(R.id.title_view, mAudioBean.name);
        mSmallRemoteView.setTextViewText(R.id.tip_view, mAudioBean.album);


        //点击播放暂停 实现的广播
        Intent playintent = new Intent(MusicService.NotificationReceiver.ACTION_STATUS_BAR);
        playintent.putExtra(MusicService.NotificationReceiver.EXTRA,
                MusicService.NotificationReceiver.EXTRA_PLAY);

        PendingIntent playPendingIntent
                = PendingIntent.getBroadcast(AudioHelper.getContext(),
                1, playintent, PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteView.setOnClickPendingIntent(R.id.play_view, playPendingIntent);
        mSmallRemoteView.setOnClickPendingIntent(R.id.play_view, playPendingIntent);


        //点击上一首发送的广播
        Intent preintent = new Intent(MusicService.NotificationReceiver.ACTION_STATUS_BAR);
        preintent.putExtra(MusicService.NotificationReceiver.EXTRA,
                MusicService.NotificationReceiver.EXTRA_PRE);
        PendingIntent prePendingIntent
                = PendingIntent.getBroadcast(AudioHelper.getContext(),
                2, preintent, PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteView.setOnClickPendingIntent(R.id.previous_view, prePendingIntent);

        //点击下一首发送的广播

        Intent nextintent = new Intent(MusicService.NotificationReceiver.ACTION_STATUS_BAR);
        nextintent.putExtra(MusicService.NotificationReceiver.EXTRA,
                MusicService.NotificationReceiver.EXTRA_NEXT);
        PendingIntent nextPendingIntent
                = PendingIntent.getBroadcast(AudioHelper.getContext(),
                3, nextintent, PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteView.setOnClickPendingIntent(R.id.next_view, nextPendingIntent);
        mSmallRemoteView.setOnClickPendingIntent(R.id.next_view, nextPendingIntent);

        //收藏广播
        Intent favourintent = new Intent(MusicService.NotificationReceiver.ACTION_STATUS_BAR);
        favourintent.putExtra(MusicService.NotificationReceiver.EXTRA,
                MusicService.NotificationReceiver.EXTRA_FAV);

        PendingIntent favourPendingIntent
                = PendingIntent.getBroadcast(AudioHelper.getContext(),
                4, favourintent, PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteView.setOnClickPendingIntent(R.id.favourite_view, favourPendingIntent);

    }

    /**
     * 更新为加载状态
     **/

    public void showLoadingStatus(AudioBean bean) {
        //防止空指针crash
        mAudioBean = bean;
        if (mRemoteView == null) return;
        Log.e("mRemoteView", "mRemoteView" + mRemoteView);
        mRemoteView.setImageViewResource(R.id.play_view, R.mipmap.note_btn_pause_white);
        mRemoteView.setTextViewText(R.id.title_view, mAudioBean.name);
        mRemoteView.setTextViewText(R.id.tip_view, mAudioBean.album);
        ImageLoaderManager.getInstance().displayImageForNotification(AudioHelper.getContext(),
                mRemoteView, R.id.image_view, mNotification, NOTIFICATION_ID, mAudioBean.albumPic);

        //更新收藏状态
        if (GreenDaoHelper.selectFavourite(mAudioBean) != null) {
            mRemoteView.setImageViewResource(R.id.favourite_view, R.mipmap.note_btn_loved);
        } else {
            mRemoteView.setImageViewResource(R.id.favourite_view, R.mipmap.note_btn_love_white);
        }

        //更新小布局
        mSmallRemoteView.setImageViewResource(R.id.play_view, R.mipmap.note_btn_pause_white);
        mSmallRemoteView.setTextViewText(R.id.title_view, mAudioBean.name);
        mSmallRemoteView.setTextViewText(R.id.tip_view, mAudioBean.album);
        ImageLoaderManager.getInstance().displayImageForNotification(AudioHelper.getContext(),
                mSmallRemoteView, R.id.image_view, mNotification, NOTIFICATION_ID, mAudioBean.albumPic);

        //更新之后要用manager 去通知
        mNotificationManager.notify(NOTIFICATION_ID, mNotification);
    }

    /**
     * 更新为播放状态
     **/

    public void showPlayStatus() {
        if (mRemoteView != null) {
            mRemoteView.setImageViewResource(R.id.play_view, R.mipmap.note_btn_pause_white);
            mSmallRemoteView.setImageViewResource(R.id.play_view, R.mipmap.note_btn_pause_white);
            mNotificationManager.notify(NOTIFICATION_ID, mNotification);
        }
    }

    /**
     * 更新暂停状态
     **/
    public void showPauseStatus() {
        if (mRemoteView != null) {
            mRemoteView.setImageViewResource(R.id.play_view, R.mipmap.note_btn_play_white);
            mSmallRemoteView.setImageViewResource(R.id.play_view, R.mipmap.note_btn_play_white);
            mNotificationManager.notify(NOTIFICATION_ID, mNotification);
        }
    }


    public void changeFavouriteStatus(boolean isFavourite) {
        if (mRemoteView != null) {
            mRemoteView.setImageViewResource(R.id.favourite_view,
                    isFavourite ? R.mipmap.note_btn_loved : R.mipmap.note_btn_love_white);
            mNotificationManager.notify(NOTIFICATION_ID, mNotification);
        }

    }


    public Notification getNotification() {
        return mNotification;
    }


}
